package com.wzy.vx.controller;

import com.wzy.vx.DTO.OrderDTO;
import com.wzy.vx.Utils.ResuletVoUtils;
import com.wzy.vx.VO.ResultVo;
import com.wzy.vx.converter.OrderForm11OrderDTOConverter;
import com.wzy.vx.enums.ResuletEnum;
import com.wzy.vx.exception.SellException;
import com.wzy.vx.form.Orderform;
import com.wzy.vx.service.BuyerService;
import com.wzy.vx.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: vx
 * @Package: com.wzy.vx.converter
 * @ClassName: BuyerOrderController
 * @Author: Administrator
 * @Description: ${description}
 * @Date: 2019/3/14 9:17
 * @Version: 1.0
 */
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {
    private static   final Logger logger = LoggerFactory.getLogger(BuyerOrderController.class);
    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;


    //1.创建订单
    @PostMapping("/create")
    public ResultVo<Map<String,String>> create(@Valid Orderform orderform,
                                               BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            // todo 前端传入数据有问题
            logger.error("【创建订单】参数不正确, orderForm={}", orderform);
            throw new SellException(ResuletEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        //
        OrderDTO orderDTO = OrderForm11OrderDTOConverter.convert(orderform);
        if (CollectionUtils.isEmpty(orderDTO.getOrderMasterList())){
            logger.error("[创建订单]购物车不能为空");
            throw new SellException(ResuletEnum.Cart_EMPTY);
        }
        OrderDTO createDTO = orderService.create(orderDTO);
        Map<String,String> result = new HashMap<>();
        result.put("orderId",createDTO.getOrderId());
        return ResuletVoUtils.success(result);
    }

    //2.订单列表
    @GetMapping("/list")
    public ResultVo<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(openid)) {
            logger.error("【查询订单列表】openid为空");
            throw new SellException(ResuletEnum.PARAM_ERROR);
        }

        PageRequest request = new PageRequest(page, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, request);
        //转换 date转成 long
        return ResuletVoUtils.success(orderDTOPage.getContent());
    }

    //3.订单详情z
    @GetMapping("/orderDetail")
    public ResultVo<OrderDTO> orderDetail(@RequestParam("openid") String openid,
                                         @RequestParam(value = "orderId") String orderId) {
        // 安全做法 防止越权

        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
        return ResuletVoUtils.success(orderDTO);
    }
    //4.取消订单
    @GetMapping("/cancel")
    public ResultVo<OrderDTO> cancel(@RequestParam("openid") String openid,
                                     @RequestParam(value = "orderId") String orderId) {
        //安全做法 防止越权
        OrderDTO order = buyerService.cannelOrder(openid,orderId );
        orderService.cannel(order);
        return ResuletVoUtils.success();
    }
}
